package com.mkx.service.impl;

import com.mkx.service.FirstCaseService;
import com.mkx.service.SecondCaseService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;

/**
 * Second Case 服务实现类
 * 使用DubboService注解将服务暴露给其他微服务
 * 使用DubboReference注解引用其他微服务
 */
@DubboService(interfaceClass = SecondCaseService.class, group = "DEV_GROUP", version = "1.0.0", timeout = 3000)
public class SecondCaseServiceImpl implements SecondCaseService {

    // 引用first-case服务，设置check=false避免启动时严格检查
    @DubboReference(interfaceClass = FirstCaseService.class, group = "DEV_GROUP", version = "1.0.0", timeout = 3000, retries = 0, check = false)
    private FirstCaseService firstCaseService;

    @Override
    public String getServiceInfo(String name) {
        return "Second Case Service - Hello, " + name;
    }

    @Override
    public String processData(String data) {
        return "Second Case Service - Processed data: " + data;
    }

    @Override
    public String callOtherService(String message) {
        try {
            // 调用first-case服务的方法
            String result = firstCaseService.getServiceInfo(message);
            return "Second Case Service called First Case Service: " + result;
        } catch (Exception e) {
            // 容错处理，提供降级方案
            System.err.println("调用FirstCaseService失败: " + e.getMessage());
            return "Second Case Service - 服务暂时不可用，已进入降级模式，请求信息: " + message;
        }
    }
}